using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI.WebControls;
using System.Web.UI;
using System.ComponentModel;

[assembly: TagPrefix("SurveyHost.Core.Controls", "shc")] 
namespace SurveyHost.Core.Controls
{
 

    /// <summary>
    /// The second chance validator will return invalid X number of times, giving the user the chance to change their mind.
    /// Controls that are to be validated MUST mark the property to be validated with the ValidationPropertyAttribute.
    /// </summary>
    [ToolboxData("<{0}:SecondChanceValidator runat=server></{0}:SecondChanceValidator>")]
    [DefaultProperty("ShouldNotEqual")]
    public class SecondChanceValidator : BaseValidator
    {
        protected override object SaveViewState()
        {
            return base.SaveViewState();
        }

		#region  Properties and variables (3)

		public int MaxRetries
        {
            get
            {
                object o = ViewState["MaxRetries"];
                if (o != null)
                    return (int)o;
                else
                    return 1;
            }
            set
            {
                ViewState["MaxRetries"] = value;
            }
        }
		
		public string ShouldNotEqual
        {
            get
            {
                object o = ViewState["ShouldNotEqual"];
                if (o == null) return string.Empty;
                return (string)o;
            }
            set
            {
                ViewState["ShouldNotEqual"] = value;
            }
        }
		
		/// <summary>
        /// The question number.
        /// </summary>
        /// <value></value>
        /// <remarks>Numbering should restart with each new measure</remarks>
        [Browsable(false),DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public int TimesAsked
        {
            get
            {
                object o = ViewState["TimesAsked"];
                if (o != null)
                    return (int)o;
                else
                    return 0;
            }
            protected set
            {
                ViewState["TimesAsked"] = value;
            }
        }
		
		#endregion 

		#region  Methods (1)

		protected override bool EvaluateIsValid()
        {
            string value = GetControlValidationValue(ControlToValidate);
            if (string.IsNullOrEmpty(value) || string.Equals(value, ShouldNotEqual,StringComparison.OrdinalIgnoreCase))
                if (TimesAsked < MaxRetries)
                {
                    TimesAsked = TimesAsked + 1;
                    return false;
                }
            return true;
        }
		
		#endregion 

    }
}
